package com.qf.springmvc02.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author nk
 * @date 2022/5/5 14:13
 */
@Controller
public class FileController {
    @RequestMapping("/download")
    public void download(String filename, HttpServletResponse response,HttpServletRequest request) throws IOException {

        //设置响应的格式  执行让浏览器使用附件attachment的形式下载文件
        response.setHeader("content-disposition","attachment;filename="+filename);
        //获取要下载的文件所在目录的真实地址
        String realPath = request.getServletContext().getRealPath("/upload");
        //构造要下载的文件对象
        File file = new File(realPath, filename);
        //读取文件内容到字节数组中
        byte[] bytes = FileUtils.readFileToByteArray(file);
        ServletOutputStream outputStream = response.getOutputStream();
        //把字节数组写入到输出流中
        outputStream.write(bytes);
        outputStream.close();
    }
    @RequestMapping("/upload")
    public String upload(MultipartFile upload, String name, Model model, HttpServletRequest request) throws IOException {
        //name 用户名字，upload 上传的文件
        String realPath = request.getServletContext().getRealPath("/");
        System.out.println(realPath);
        //2、在上述路径下创建一个upload文件夹
        File uploadDir = new File(realPath, "upload");
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        //3、获取上传文件的名字
        String filename = upload.getOriginalFilename();
        //3.1 如果只允许用户上传图片，不能上传其他文件   xxx.jpg   xxx.png   xxx.bmp   xxx.gif
        //获取文件的后缀名
        String suffix = filename.substring(filename.indexOf("."));
        if (suffix.equalsIgnoreCase(".jpg")
                || suffix.equalsIgnoreCase(".png")
                || suffix.equalsIgnoreCase(".bmp")
                || suffix.equalsIgnoreCase(".gif")) {
            //3.2重命名文件
            String uuid = UUID.randomUUID().toString();
            uuid = uuid.replace("-", "") + suffix;
            //4、IO流操作进行文件复制
            upload.transferTo(new File(uploadDir, uuid));
            model.addAttribute("msg", "文件上传成功");
            //5、在页面上显示一下上传文件目录中都有哪些文件
            String[] fileNames = uploadDir.list();
            model.addAttribute("fileNames", fileNames);
            return "welcome";
        } else {
            model.addAttribute("msg", "上传的不是图片");
            return "upload";
        }
    }

    @RequestMapping("/upload2")
    public String upload2(MultipartFile[] uploadFiles, String name, Model model, HttpServletRequest request) throws IOException {
        //name 用户名字，upload 上传的文件
        String realPath = request.getServletContext().getRealPath("/");
        System.out.println(realPath);
        //2、在上述路径下创建一个upload文件夹
        File uploadDir = new File(realPath, "upload");
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        for (MultipartFile upload :
                uploadFiles) {
            //3、获取上传文件的名字
            String filename = upload.getOriginalFilename();
            //3.1 如果只允许用户上传图片，不能上传其他文件   xxx.jpg   xxx.png   xxx.bmp   xxx.gif
            //获取文件的后缀名
            String suffix = filename.substring(filename.indexOf("."));
            if (suffix.equalsIgnoreCase(".jpg") || suffix.equalsIgnoreCase(".png")
                    || suffix.equalsIgnoreCase(".bmp") || suffix.equalsIgnoreCase(".gif")) {
                //3.2重命名文件
                String uuid = UUID.randomUUID().toString();
                uuid = uuid.replace("-", "") + suffix;
                //4、IO流操作进行文件复制
                upload.transferTo(new File(uploadDir, uuid));
                model.addAttribute("msg", "文件上传成功");
                //5、在页面上显示一下上传文件目录中都有哪些文件
                String[] fileNames = uploadDir.list();
                model.addAttribute("fileNames", fileNames);
            } else {
                model.addAttribute("msg", "上传的不是图片");
                return "upload";
            }
        }
        return "welcome";
    }
}
